package test5;

import test4.Cat;
import test4.Dog;
import test4.Person;
import java.util.Arrays;

public class test55 {
    // 2.新建一个新的工程并在工程当中使用这个Jar包，生成与Jar包中Main中相同结果
    public static void demoReuseJar() {
        System.out.println("2.复用Jar包类演示");
        Cat cat = new Cat();
        Dog dog = new Dog();
        Person person = new Person();

        person.feed(cat);
        person.feed(dog);

        System.out.println();
        person.catchAnimal(cat);
        person.catchAnimal(dog);
    }

    // 3.将字符串"60,120,30,90,180"转化为double数组，使用数组服务类进行从小到大排序，使用Math类计算arr[0]度的sin值
    public static void demoArrayAndMath() {
        System.out.println("\n3.数组处理与Math计算");
        String numStr = "60,120,30,90,180";
        String[] strArr = numStr.split(",");
        double[] doubleArr = new double[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            doubleArr[i] = Double.parseDouble(strArr[i]);
        }

        Arrays.sort(doubleArr);
        System.out.println("排序后的double数组：" + Arrays.toString(doubleArr));

        double minAngle = doubleArr[0];
        double radians = Math.toRadians(minAngle);
        double sinValue = Math.sin(radians);
        System.out.printf("%.1f度的sin值（保留4位小数）：%.4f%n", minAngle, sinValue);
    }

    // 4.使用String类对象（A dog is a kind of animal）来生成StringBuffer类对象，并把dog替换成cat
    public static void demoStringAndStringBuffer() {
        System.out.println("\n4.String与StringBuffer操作");
        String originalStr = "A dog is a kind of animal";
        System.out.println("原始字符串：" + originalStr);

        StringBuffer sb = new StringBuffer(originalStr);
        sb.replace(2, 5, "cat");
        System.out.println("替换后字符串：" + sb.toString());
    }

    public static void main(String[] args) {
        demoReuseJar();
        demoArrayAndMath();
        demoStringAndStringBuffer();
    }
}